from rest_framework.authentication import BasicAuthentication
from rest_framework.response import Response
from utils.tooljwt import tooljwt

from user import models


class JWTAuthenticationss(BasicAuthentication):
    def authenticate(self, request):
        tokendata = request.META.get('HTTP_AUTHORIZATION', None)
        if tokendata:
            token = tokendata.split(' ')[1]
            userdata = tooljwt.checktoken(token)
            if userdata:
                userid = userdata['userid']
                user = models.User.objects.get(id=userid)
                return user, token
        else:
            return None, None

    def authenticate_header(self, request):
        return 'Bearer realm="api"'
